package com.work.demo.utils;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.work.demo.entity.User;

import java.util.HashMap;

/**
 * 用户登录数据
 * @author jianglou
 */
public class UserUtils {

     /** 登录的数据 **/
     private final static ThreadLocal<User> LOGIN_DTO_THREAD_LOCAL = new TransmittableThreadLocal<User>();

     /**
      * 查询登录信息
      * @return LoginDTO
      */
     public static User get(){
          return LOGIN_DTO_THREAD_LOCAL.get();
     }

     /**
      * 设置登录信息
      * @param dto
      */
     public static void set(User dto){
          LOGIN_DTO_THREAD_LOCAL.set(dto);
     }

     /**
      * 清除登录信息
      */
     public static void remove(){
          LOGIN_DTO_THREAD_LOCAL.remove();
     }

     /**
      * 通过id 生成account，重复几率很小，但是需要有注册和重试机制
      *
      * @param id
      * @return
      */
     public static String generateAccount(Long id) {

          HashMap<Character, Character> tranMap = new HashMap<>();
          tranMap.put('0', 'v');
          tranMap.put('6', 'w');
          tranMap.put('7', 'x');
          tranMap.put('8', 'y');
          tranMap.put('9', 'z');
          String account = Long.toString(id, 31);
          for (Character ch : tranMap.keySet()) {
               account = account.replace(ch, tranMap.get(ch));
          }
          return account;
     }
}
